package com.amit.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="forgotpw",urlPatterns={"/forgotpw.jlc"})
public class ForgotPWServlet extends HttpServlet {
	
	UserService us=null;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		
		us=new UserService();
		
		
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String em=request.getParameter("email");
		String pw=us.getPasswordByEmail(em);
		String result=" ";
		if(pw!=null)
		{
			result="pwstatus.jsp";
			String sub="Your Password Here";
			String body="<h1>Hi Your Password is"+" "+pw+"</h1>";
			MailService.sendMail(em, sub, body);
		}
		else
		{
			String msg="Invalid Email-try Again";
			request.setAttribute("MSG", msg);
			result="forgotpw.jsp";
		}
		
		RequestDispatcher rd=request.getRequestDispatcher(result);
		rd.forward(request, response);
		
		
	}
	

}
